package com.aspose.cells.b.a.d;

import com.aspose.cells.Encoding;

public class zn extends zq {
   private zm b;
   private Encoding c;
   private com.aspose.cells.b.a.g.zb d;
   private byte[] e;
   private char[] f;
   private byte[] g;
   private int h;
   private int i;
   private int j;
   private int k;
   private int l;
   private boolean m;
   private boolean n;
   private boolean o;

   zn() {
   }

   public zn(zm var1) {
      this(var1, true);
   }

   public zn(zm var1, boolean var2) {
      this(var1, Encoding.getUTF8(), var2, 1024);
   }

   public zn(zm var1, Encoding var2) throws Exception {
      this(var1, var2, true, 1024);
   }

   public zn(zm var1, Encoding var2, boolean var3, int var4) {
      if (var1 != null && var2 != null) {
         if (var4 <= 0) {
            throw new IndexOutOfBoundsException("bufferSize");
         } else {
            this.a(var1, var2, var3, var4);
         }
      } else {
         throw new NullPointerException(var1 == null ? "stream" : "encoding");
      }
   }

   public zn(String var1, Encoding var2) throws Exception {
      com.aspose.cells.a.e.zg.b(var1, "path");
      ze var3 = new ze(var1, false, false);
      if (var2 == null) {
         throw new NullPointerException("encoding");
      } else {
         this.a(var3, var2, true, 1024);
      }
   }

   private void a(zm var1, Encoding var2, boolean var3, int var4) {
      this.b = var1;
      this.c = var2;
      this.d = var2.c();
      if (var4 < 128) {
         var4 = 128;
      }

      this.e = new byte[var4];
      this.l = var4 * 2;
      this.f = new char[this.l];
      this.j = 0;
      this.k = 0;
      this.m = var3;
      this.g = var2.g();
      this.n = this.g.length > 0;
      this.o = false;
   }

   public void a() throws Exception {
      this.a(true);
   }

   protected void a(boolean var1) throws Exception {
      try {
         if (var1 && this.b != null) {
            this.b.a();
         }
      } finally {
         if (this.b != null) {
            this.b = null;
            this.c = null;
            this.d = null;
            this.e = null;
            this.f = null;
            this.h = 0;
            this.i = 0;
         }

      }

   }

   public zm b() {
      return this.b;
   }

   public int c() throws Exception {
      if (this.b == null) {
         throw new IllegalStateException("StreamReader Closed");
      } else {
         return this.h != this.i || !this.o && this.i() != 0 ? this.f[this.h] : -1;
      }
   }

   public int d() throws Exception {
      if (this.b == null) {
         throw new IllegalStateException("StreamReader Closed");
      } else if (this.h == this.i && this.i() == 0) {
         return -1;
      } else {
         char var1 = this.f[this.h];
         ++this.h;
         return var1;
      }
   }

   public int a(char[] var1, int var2, int var3) throws Exception {
      if (this.b == null) {
         throw new IllegalStateException("StreamReader Closed");
      } else if (var1 == null) {
         throw new NullPointerException("buffer");
      } else if (var2 >= 0 && var3 >= 0) {
         if (var1.length - var2 < var3) {
            throw new IllegalArgumentException("index and count don't match buffer length");
         } else {
            int var4 = 0;
            boolean[] var5 = new boolean[]{false};

            while(var3 > 0) {
               int var6 = this.i - this.h;
               if (var6 == 0) {
                  var6 = this.a(var1, var2 + var4, var3, var5);
               }

               if (var6 == 0) {
                  break;
               }

               if (var6 > var3) {
                  var6 = var3;
               }

               if (!var5[0]) {
                  System.arraycopy(this.f, this.h, var1, var2 + var4, var6);
                  this.h += var6;
               }

               var4 += var6;
               var3 -= var6;
               if (this.o) {
                  break;
               }
            }

            return var4;
         }
      } else {
         throw new IndexOutOfBoundsException(var2 < 0 ? "index" : "count");
      }
   }

   public String e() throws Exception {
      if (this.b == null) {
         throw new IllegalStateException("StreamReader Closed");
      } else {
         StringBuilder var1 = new StringBuilder(this.i - this.h);

         do {
            var1.append(this.f, this.h, this.i - this.h);
            this.h = this.i;
            this.i();
         } while(this.i > 0);

         return var1.toString();
      }
   }

   private void a(int var1) {
      assert this.j >= var1 : "CompressBuffer was called with a number of bytes greater than the current buffer length.  Are two threads using this StreamReader at the same time?";

      System.arraycopy(this.e, var1, this.e, 0, this.j - var1);
      this.j -= var1;
   }

   private void g() throws Exception {
      if (this.j >= 2) {
         this.m = false;
         boolean var1 = false;
         if (this.e[0] == -2 && this.e[1] == -1) {
            this.c = Encoding.a(true, true);
            this.a(2);
            var1 = true;
         } else if (this.e[0] == -1 && this.e[1] == -2) {
            if (this.j >= 4 && this.e[2] == 0 && this.e[3] == 0) {
               this.c = Encoding.b(false, true);
               this.a(4);
            } else {
               this.c = Encoding.a(false, true);
               this.a(2);
            }

            var1 = true;
         } else if (this.j >= 3 && this.e[0] == -17 && this.e[1] == -69 && this.e[2] == -65) {
            this.c = Encoding.getUTF8();
            this.a(3);
            var1 = true;
         } else if (this.j >= 4 && this.e[0] == 0 && this.e[1] == 0 && this.e[2] == -2 && this.e[3] == -1) {
            this.c = Encoding.b(true, true);
            var1 = true;
         } else if (this.j >= 5 && this.e[0] == 43 && this.e[1] == 47 && this.e[2] == 118 && this.e[3] == 56 && this.e[4] == 45) {
            if (!this.c.equals(Encoding.getUTF7())) {
               this.c = Encoding.getUTF7();
               var1 = true;
            }

            this.a(5);
         } else if (this.j == 2) {
            this.m = true;
         }

         if (var1) {
            this.d = this.c.c();
            this.l = this.d.a(this.e.length + 2);
            this.f = new char[this.l];
         }

      }
   }

   private boolean h() {
      if (!this.n) {
         return this.n;
      } else {
         assert this.k <= this.g.length : "_compressPreamble was called with the current bytePos greater than the preamble buffer length.  Are two threads using this StreamReader at the same time?";

         int var1 = this.j >= this.g.length ? this.g.length - this.k : this.j - this.k;

         for(int var2 = 0; var2 < var1; ++this.k) {
            if (this.e[this.k] != this.g[this.k]) {
               this.k = 0;
               this.n = false;
               break;
            }

            ++var2;
         }

         assert this.k <= this.g.length : "possible bug in _compressPreamble.  Are two threads using this StreamReader at the same time?";

         if (this.n && this.k == this.g.length) {
            this.a(this.g.length);
            this.k = 0;
            this.n = false;
            this.m = false;
         }

         return this.n;
      }
   }

   private int i() throws Exception {
      this.i = 0;
      this.h = 0;
      if (!this.n) {
         this.j = 0;
      }

      do {
         if (this.n) {
            assert this.k <= this.g.length : "possible bug in _compressPreamble.  Are two threads using this StreamReader at the same time?";

            int var1 = this.b.a(this.e, this.k, this.e.length - this.k);

            assert var1 >= 0 : "Stream.Read returned a negative number!  This is a bug in your stream class.";

            if (var1 == 0) {
               if (this.j > 0) {
                  this.i += this.d.a(this.e, 0, this.j, this.f, this.i);
               }

               return this.i;
            }

            this.j += var1;
         } else {
            assert this.k == 0 : "bytePos can be non zero only when we are trying to _checkPreamble.  Are two threads using this StreamReader at the same time?";

            this.j = this.b.a(this.e, 0, this.e.length);

            assert this.j >= 0 : "Stream.Read returned a negative number!  This is a bug in your stream class.";

            if (this.j == 0) {
               return this.i;
            }
         }

         this.o = this.j < this.e.length;
         if (!this.h()) {
            if (this.m && this.j >= 2) {
               this.g();
            }

            this.i += this.d.a(this.e, 0, this.j, this.f, this.i);
         }
      } while(this.i == 0);

      return this.i;
   }

   private int a(char[] var1, int var2, int var3, boolean[] var4) throws Exception {
      this.i = 0;
      this.h = 0;
      if (!this.n) {
         this.j = 0;
      }

      int var5 = 0;
      var4[0] = var3 >= this.l;

      do {
         if (this.n) {
            assert this.k <= this.g.length : "possible bug in _compressPreamble.  Are two threads using this StreamReader at the same time?";

            int var6 = this.b.a(this.e, this.k, this.e.length - this.k);

            assert var6 >= 0 : "Stream.Read returned a negative number!  This is a bug in your stream class.";

            if (var6 == 0) {
               if (this.j > 0) {
                  if (var4[0]) {
                     var5 += this.d.a(this.e, 0, this.j, var1, var2 + var5);
                     this.i = 0;
                  } else {
                     var5 = this.d.a(this.e, 0, this.j, this.f, var5);
                     this.i += var5;
                  }
               }

               return var5;
            }

            this.j += var6;
         } else {
            assert this.k == 0 : "bytePos can be non zero only when we are trying to _checkPreamble.  Are two threads using this StreamReader at the same time?";

            this.j = this.b.a(this.e, 0, this.e.length);

            assert this.j >= 0 : "Stream.Read returned a negative number!  This is a bug in your stream class.";

            if (this.j == 0) {
               return var5;
            }
         }

         this.o = this.j < this.e.length;
         if (!this.h()) {
            if (this.m && this.j >= 2) {
               this.g();
               var4[0] = var3 >= this.l;
            }

            this.h = 0;
            if (var4[0]) {
               var5 += this.d.a(this.e, 0, this.j, var1, var2 + var5);
               this.i = 0;
            } else {
               var5 = this.d.a(this.e, 0, this.j, this.f, var5);
               this.i += var5;
            }
         }
      } while(var5 == 0);

      this.o &= var5 < var3;
      return var5;
   }

   public String f() throws Exception {
      if (this.b == null) {
         throw new IllegalStateException("StreamReader Closed");
      } else if (this.h == this.i && this.i() == 0) {
         return null;
      } else {
         StringBuilder var1 = null;

         do {
            int var2 = this.h;

            do {
               char var3 = this.f[var2];
               if (var3 == '\r' || var3 == '\n') {
                  String var4;
                  if (var1 != null) {
                     var1.append(this.f, this.h, var2 - this.h);
                     var4 = var1.toString();
                  } else {
                     var4 = new String(this.f, this.h, var2 - this.h);
                  }

                  this.h = var2 + 1;
                  if (var3 == '\r' && (this.h < this.i || this.i() > 0) && this.f[this.h] == '\n') {
                     ++this.h;
                  }

                  return var4;
               }

               ++var2;
            } while(var2 < this.i);

            var2 = this.i - this.h;
            if (var1 == null) {
               var1 = new StringBuilder(var2 + 80);
            }

            var1.append(this.f, this.h, var2);
         } while(this.i() > 0);

         return var1.toString();
      }
   }
}
